package com.gome.extract;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.gome.extract.common.AppConfiguer;
import com.gome.extract.common.GlobalUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;


public class DBOperator extends GlobalUtils
{
	private static Logger logger = Logger.getLogger(DBOperator.class);
	
	private Mongo mMongo = null;
	private HTMLScanner mScan = null;
	private String mInvertName = "";
	
    public DBOperator()
    {
    	try
    	{
			String host = AppConfiguer.getStringByKey("mongodb_host");
			String port = AppConfiguer.getStringByKey("mongodb_port");
			mMongo = new Mongo(host, Integer.parseInt(port));
    	}
    	catch(Exception e)
    	{
    		logger.error(AppConfiguer.getTrace(e));
    		System.exit(-1);
    	}
    }

	public boolean build(String dbName, String collectName, boolean useHBaseTransit)
	{
		if (useHBaseTransit) {
			SpiderDataDriver driver = new SpiderDataDriver();
			String[] args = new String[1];
			args[0] = collectName;
			try {
				driver.run(args);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				logger.error(AppConfiguer.getTrace(e));
			}
		}
		
		DB mDB = mMongo.getDB(dbName);
		DBCollection mCollector = mDB.getCollection(collectName);
		String dictName = AppConfiguer.getStringByKey("mid_datapath")  + "/" + collectName + ".dic";
		String dataName = AppConfiguer.getStringByKey("mid_datapath") + "/" + collectName + ".dat";
		mScan = new HTMLScanner(dictName);
		Vector<String> vec = new Vector<String>();
		BasicDBObject query = new BasicDBObject();
		DBCursor cur = mCollector.find(query);
		logger.info("Build " + dbName + "." + collectName + " count : " + cur.count());
		
		int i = 0;
		try
		{
			HashMap<String, String> hsIdx = new HashMap<String, String>();
			FileOutputStream out = new FileOutputStream(dataName);
			long startInit = System.currentTimeMillis();
			String pid = "";
			String id = "";
			String result = "";
			String stepID = "";
//			String page = "";
		    while (cur.hasNext()) 
		    {  	
		    	i += 1;
//		    	if(i==30000)
//		    	{
//		    		long endInit = System.currentTimeMillis();						
//			 	   	long chaInit = endInit - startInit;
//			 	   	System.out.println("已完成:" + i + "\t耗时 " + chaInit/1000 + "秒");
//		    		break;
//		    	}
				if(i%10000==0)
				{
					long endInit = System.currentTimeMillis();						
			 	   	long chaInit = endInit - startInit;
			 	   	logger.info("Build " + dbName + "." + collectName + "已完成:" + i + "\t耗时 " + chaInit/1000 + "秒");
				}
		    	BasicDBObject obj = (BasicDBObject) cur.next(); 
		    	if(obj != null)
		    	{ 
		    		id = obj.getString("id");
		    		if(id==null)
		    		{
		    			id = obj.getString("_id");
		    			continue;
		    		}
	    			id = id.toLowerCase();
	    			
	    			stepID = obj.getString("stepID");
	    		    			
	    			String page = obj.getString("pageSource");
	    			if(page==null)
	    				continue;
    				page = page.replaceAll("\t", " ");
	    			
		    		BasicDBObject dbj = (BasicDBObject)obj.get("parent");
		    		StringBuffer sb = new StringBuffer();
		    		if(dbj!=null)
		    		{
		    			pid = dbj.getString("id");
			    		if(pid!=null)
			    			pid = pid.toLowerCase();
			    		else
			    			pid = "0";
		    		}
		    		else
		    		{
		    			pid = "0";
		    		}

		    		sb.append(stepID).append(TAB).append(id).append(TAB).append(pid).append(TAB).append(mScan.scan(page)).append("\n");

	    			out.write(sb.toString().getBytes("UTF8"));
	    			
	    		}
		    }

		    mScan.writeDict();
		    out.close();
		    return true;
		}
		catch(Exception e)
		{
			logger.error(AppConfiguer.getTrace(e));
			return false;		
		}
	}
	
	public void insert(String dbName, String collectName, Vector<DBObject> vec)
	{
		try
		{
			DB mDB = mMongo.getDB(dbName);
			DBCollection mCollector = mDB.getCollection(collectName);
			for(int i=0; i<vec.size(); ++i)
			{
				mCollector.save(vec.elementAt(i));
			}
		}
		catch(Exception e)
		{
			
		}
	}
	
	public Vector<String> read(String dbName, String collectName, String stepID, String nameValue)
	{
		try
		{
			DB mDB = mMongo.getDB(dbName);
			DBCollection mCollector = mDB.getCollection(collectName);
			DBCursor cur = mCollector.find(new BasicDBObject("name",nameValue));
			
			Vector<String> vec = new Vector<String>();
			
			String page = "";
			String temp = "";
	
		    while(cur.hasNext()) 
		    {
		    	BasicDBObject obj = (BasicDBObject) cur.next(); 
		    	String step = obj.getString("stepID");
		    	if(step.equals(stepID)==false)
		    		continue;
		    	
		    	List<DBObject> lst = (List<DBObject>)obj.get("fields");
		    	for(int i=0; i<lst.size(); ++i)
		    	{
		    		String key = lst.get(i).get("key").toString();
		    		List<DBObject> lst1 = (List<DBObject>)lst.get(i).get("keys");   	
		    		String k = "";
		    		int t = 0;
		    		for(int j=0; j<lst1.size()&&t<3; ++j, ++t)
		    		{
		    			if(lst1.get(j).get("classvalue")==null)
		    			{
		    				--t;
		    				continue;
		    			}
		    			k += lst1.get(j).get("classvalue").toString().toLowerCase().split(" ")[0] + CON;
		    		}
		    		k = k.substring(0, k.length()-2);
		    		vec.add(key + TAB + k);
//		    		print(k);
		    	}
		    }
		    return vec;
			
		}
		catch(Exception e)
		{
			return null;
		}
		
	}
	
//	
//    public static void main(String[] args)
//    {
//    	long startInit = System.currentTimeMillis();
//    	
//    	String ivt = new Global().filePath + "/dianping_com_xiuxianyule_pages.dat";
//    	String dic = new Global().filePath + "/dianping_com_xiuxianyule_pages.dic";
//    	DBOperator op = new DBOperator();
//        op.build("flowcrawl", "dianping_com_xiuxianyule_pages");
//        
//        long endInit = System.currentTimeMillis();
// 	   	long chaInit = endInit - startInit;
// 	   	System.out.println("共耗时 " + chaInit/1000 + "秒");
//    }
}





